package com.mge.humitureproj.utils;

import lombok.extern.slf4j.Slf4j;

import java.io.IOException;

/**
 * 语音播报工具类
 */
@Slf4j
public class VoiceAnnouncementsUtil {

    /**
     * 播报指定文本
     * @param text 要播报的文本
     */
    public static void announce(String text) {
        // 语音播报逻辑
        String os = System.getProperty("os.name").toLowerCase();
        try {
            if (os.contains("win")) {
                // Windows系统
                // 使用完整路径调用 powershell.exe
                String powerShellPath = System.getenv("SystemRoot") + "\\System32\\WindowsPowerShell\\v1.0\\powershell.exe";
                Process process = Runtime.getRuntime().exec(new String[] {
                        powerShellPath,
                        "-Command",
                        "Add-Type -AssemblyName System.Speech; (New-Object System.Speech.Synthesis.SpeechSynthesizer).Speak('" + text + "');"
                });
            }  else {
                    // 播放音频
                    Process aplay = new ProcessBuilder(
                            "aplay", " /opt/mge/waring.wav"
                    ).start();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
